/* 
 *  Copyright (C) 2008 cooleyes
 *  eyes.cooleyes@gmail.com 
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#ifndef __SIRENS2_AUDIO_PLAYER_H__
#define __SIRENS2_AUDIO_PLAYER_H__

#include <stdint.h>
#include <pspsdk.h>

#include "audio_parser.h"
#include "playback_info.h"
#include "../common/directory.h"

class AudioPlayer {

private:
  AudioParser* parser;
  bool parser_open;
  bool player_open;
  bool srcchannel_open;
  
  
  int64_t start_time;
  int64_t end_time;
  int64_t current_time;
  
  void* cached_pcm_buffer;
  int32_t cached_samples;
  
  int32_t pcm_samples;
  void* pcm_buffer[2];
  int32_t pcm_buffer_index;
  
  SceUID play_thread;
  
  bool play_pause;
  bool play_eof;
  bool play_seekable;
  bool play_seek;
  
  int64_t current;
  int64_t duration;
  int64_t seek_to;
  
  AudioPlayer(AudioParser* parser, bool play_seekable = true);
  void initialize();
  void finalize();
  
  void loop();
  
  static int agent(SceSize input_length, void *input);
  
  
public:  
  ~AudioPlayer();
  
  static char* init_hw_codec();
  
  static AudioPlayer* create_audio_player(file_type_enum type);
  
  char* open(const char* filename, int64_t start_time, int64_t end_time);
  
  void close();
  
  char* play();
  
  void stop();
  
  void pause();
  
  void resume();
  
  bool eof();
  
  bool is_seekable();
  
  void seek(int64_t seek_time);
  
  int64_t get_duration();
  int64_t get_current();
  
};

#endif